package com.online.web.signature;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * aes工具
 *
 * @author xiangtianlei
 * @date 2023/08/10
 */
public class AESUtils {
    private static final String AES = "AES";
    private static final String UTF8 = "UTF-8";

    /**
     * AES加密
     *
     * @param content     明文内容
     * @param private_key 秘钥
     * @return 加密后的密文
     * @throws DecoderException 译码器异常
     */
    private static byte[] encrypt(String content, String private_key) throws DecoderException {
        try {
            // 将秘钥进行Hex解码
            byte[] encodeFormat = Hex.decodeHex(private_key.toCharArray());
            SecretKeySpec key = new SecretKeySpec(encodeFormat, AES);

            // 创建AES加密算法的Cipher对象
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

            // 将明文内容转换为字节数组
            byte[] byteContent = content.getBytes(UTF8);

            // 用秘钥初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, key);

            // 执行加密操作
            byte[] result = cipher.doFinal(byteContent);

            return result;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
                 UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES解密
     *
     * @param contents   密文内容
     * @param privateKey 秘钥
     * @return 解密后的明文
     * @throws DecoderException 译码器异常
     */
    private static byte[] decrypt(String contents, String privateKey) throws DecoderException {
        try {
            // 将密文进行Hex解码
            byte[] content = Hex.decodeHex(contents.toCharArray());

            // 将秘钥进行Hex解码
            byte[] encodeFormat = Hex.decodeHex(privateKey);
            SecretKeySpec key = new SecretKeySpec(encodeFormat, AES);

            // 创建AES加密算法的Cipher对象
            Cipher cipher = Cipher.getInstance(AES);

            // 用秘钥初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, key);

            // 执行解密操作
            byte[] result = cipher.doFinal(content);

            return result;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
                 IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 加密
     * AES加密
     *
     * @param context    明文内容
     * @param privateKey 私钥
     * @return 加密后的密文
     */
    public static String encryption(String context, String privateKey) {
        String encryptResultStr = null;
        try {
            byte[] encryptResult = encrypt(context, privateKey);
            // 将加密后的字节数组转换为Hex编码的字符串
            encryptResultStr = Hex.encodeHexString(encryptResult);
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        return encryptResultStr;
    }

    /**
     * 解密
     * AES解密
     *
     * @param context    密文内容
     * @param privateKey 私钥
     * @return 解密后的明文
     */
    public static String decryption(String context, String privateKey) {
        String result = null;
        try {
            byte[] decryptResult = decrypt(context, privateKey);
            result = new String(decryptResult, UTF8);
        } catch (DecoderException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws UnsupportedEncodingException, DecoderException {
        // 加密内容
        String content = "{\"actId\":\"5522522112\",\"tasks\":[{\"taskId\":\"123456\",\"userPhone\":\"152xxxxx90\"}]}";
        String key = "2514d8a41c7bf5583ea570047a2836d2";
        // AES加密解密秘钥
        String encryptedText = encryption(content, key);
        // 加密
        System.out.println("加密::" + encryptedText);
        String decryptedText = decryption(encryptedText, key);
        // 解密
        System.out.println("解密::" + decryptedText);
    }
}
